#!usr/bin/env python3
# -*- coding:utf-8 -*-

from . import api as apiBP
from .authentication import auth
from ..models import Post,User
from flask import jsonify

#http://127.0.0.1:5000/api/v1.0

#获取所有博客
@apiBP.route("/posts/",methods=['GET'])
def get_posts():
	posts = Post.query.order_by(Post.timestamp.desc()).all()
	rep = jsonify({"posts":[post.to_json() for post in posts]})
	print("rep=",rep)
	return rep

@apiBP.route("/post/<int:id>",methods=['GET'])
def get_post_by_id(id):
	post = Post.query.get_or_404(id)
	return jsonify({"post":post.to_json()})

#根据博客id,获取评论数
@apiBP.route("/post/<int:id>/comments",methods=['GET'])
def get_posts_comments(id):
	post = Post.query.get_or_404(id)
	if post:
		comments = post.getDisabledComments()
		return jsonify({"comments":[comment.to_json() for comment in comments]})
	return jsonify({"comments":None})


#获取某个用户的博客列表
@apiBP.route("/user/<int:id>/posts")
def get_user_post(id):
	u = User.query.get_or_404(id)
	posts = u.posts
	return jsonify({"post":[post.to_json() for post in posts]})